package com.study.talk.module.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.talk.module.entity.Moment;
import com.study.talk.module.entity.MomentComment;
import com.study.talk.module.entity.MomentLike;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.entity.resp.MomentResponse;
import com.study.talk.module.mapper.MomentCommentMapper;
import com.study.talk.module.mapper.MomentLikeMapper;
import com.study.talk.module.mapper.MomentMapper;
import com.study.talk.module.service.FriendService;
import com.study.talk.module.service.MomentService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thor
 * @since 2024-3-13
 */
@Service
@Transactional
public class MomentServiceImpl extends ServiceImpl<MomentMapper,Moment> implements MomentService {


    @Autowired
    private MomentMapper momentMapper;

    @Autowired
    private MomentLikeMapper momentLikeMapper;

    @Autowired
    private MomentCommentMapper momentCommentMapper;


    @Autowired
    private FriendService friendService;

    @Override
    public ArrayList<MomentResponse> getFriendMomentList(Integer userId) {
        List<UserInfo> userList = friendService.getUserInfoList(userId);
        ArrayList<Moment> momentList = momentMapper.queryMomentListByUserId(userId);
        for (UserInfo user : userList) {
            momentList.addAll(momentMapper.queryMomentListByUserId(user.getId()));
        }
        return getArrayList(momentList,userId);
    }

    @Override
    public ArrayList<MomentResponse> getMyMomentList(Integer userId) {
        ArrayList<Moment> momentList = momentMapper.queryMomentListByUserId(userId);
        return getArrayList(momentList,userId);
    }

    private ArrayList<MomentResponse> getArrayList(ArrayList<Moment> momentList,Integer userId) {
        ArrayList<MomentResponse> results = new ArrayList<>();
        for (Moment moment : momentList) {
            ArrayList<MomentLike> momentLikes = momentLikeMapper.queryListByMomentId(moment.getId());
            ArrayList<MomentComment> momentComments = momentCommentMapper.queryListByMomentId(moment.getId());
            MomentLike momentLike = momentLikeMapper.queryMomentLikeByUserIdAndMomentId(moment.getId(), userId);
            if (ObjectUtils.isNotEmpty(momentLike)){
                results.add(new MomentResponse(moment,momentLike.getLikeState(),momentLikes,momentComments));
            }else {
                results.add(new MomentResponse(moment,0,momentLikes,momentComments));
            }

        }
        return (ArrayList<MomentResponse>) results.stream().sorted((a, b) -> b.getCreateTime().compareTo(a.getCreateTime())).collect(Collectors.toList());
    }
}
